part of shop;

class ImagePickerPage extends StatefulWidget {
  ImagePickerPage();
  // final String title;

  @override
  State<StatefulWidget> createState() {
    return _ImagePickerPageState();
  }
}

class _ImagePickerPageState extends State<ImagePickerPage> {
  // final picker = ImagePicker();
  // late File? _image;
  // Future getImage() async {
  //   final pickedFile = await picker.pickImage(source: ImageSource.camera);
  //
  //   setState(() {
  //     _image = File(pickedFile);
  //   });
  // }

  Widget img = const Text('通过相机拍照');
  getImage() async {
    final ImagePicker _picker = ImagePicker();
    final XFile? image = await _picker.pickImage(source: ImageSource.camera);
    if (image != null) {
      var data = await image.readAsBytes();
      setState(() {
        img = Image.memory(data);
      });
      SharedPreferences uPrefs = await SharedPreferences.getInstance();
      uPrefs.setString("coverimgpath", image.path);
    } else {
      setState(() {
        img = const Text('未选择图片！');
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('上传图片'),
      ),
      body: Center(
        child: img,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getImage,
        tooltip: '拍照',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }
}
